package com.api.controller;


import com.api.common.utils.Result;
import com.api.entity.Cate;
import com.api.service.ICateService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-03
 */
@RestController
@RequestMapping("/api/cate")
@AllArgsConstructor
public class CateController {

    private final  ICateService cateService;

    //展示
    @GetMapping
    public  Result list(@RequestParam Integer fid){
        LambdaQueryWrapper<Cate> lwq = new LambdaQueryWrapper<>();
        lwq.eq(Cate::getFid,fid);
        List<Cate> list = cateService.list(lwq);
        if (list.isEmpty()){
            return new Result().ok(new ArrayList<>());
        }
//        for (Cate cate : list) {
//            LambdaQueryWrapper<Cate> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(Cate::getFId,cate.getId());
//            List<Cate> cateList = cateService.list(wrapper);
//            cate.setCateList(cateList);
//        }
        return  new Result().ok(list);
    }




    //创建
    @PostMapping
    public Result save(@Validated  @RequestBody Cate cate){
        cateService.save(cate);
        return new Result();
    }
    //修改
    @PutMapping
    public Result update(@Validated  @RequestBody Cate cate){
        cateService.updateById(cate);
        return new Result();
    }

    //删除
    @DeleteMapping("{id}")
    public Result update(@PathVariable Integer id){
        cateService.removeById(id);
        return new Result();
    }
}
